// 实现这个项目的构建任务
const loadGruntTasks = require('load-grunt-tasks')
const sass = require('sass')
module.exports = grunt => {
  // 初始化配置
  grunt.initConfig({
    // 删除dist目录
    clean: ['dist'],
    // 处理sass文件
    sass: {
      options: {
        implementation: sass
      },
      all: {
        expand: true,
        cwd: "src",
        src: "assets/**/*.scss",
        dest: "dist",
        ext: '.css'
      }
    },
    // 压缩css文件
    cssmin: {
      options: {
        mergeIntoShorthands: false,
        roundingPrecision: -1
      },
      target: {
        expand: true,
        cwd: 'dist',
        src: '**/*.css',
        dest: 'dist',
        ext: '.min.css'
      }
    },
    // babel 转换 es6+ 新特性
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      dist: {
        expand: true,
        cwd: 'src',
        src: 'assets/**/*.js',
        dest: 'dist',
        ext: '.js'
      }
    },
    // js压缩
    uglify: {
      options: {
        mangle: false
      },
      uglifyJs: {
        expand: true,
        cwd: 'dist',
        src: '**/*.js',
        dest: 'dist',
        ext: '.min.js'
      }
    },
    // 图片压缩
    imagemin: {
      dist: {
        options: {
          optimizationLevel: 3 //定义 PNG 图片优化水平
        },
        files: [{
          expand: true,
          cwd: 'src',
          src: ['assets/images/**.*', 'assets/fonts/**.*'],
          dest: 'dist'
        }]
      }
    },
    // html模板渲染
    swigtemplates: {
      options: {
        templatesDir: 'src/*.html'
      },
      html: {
        context: require('./data'),
        dest: 'dist/src',
        src: ['src/**.html']
      }
    },
    concat: {
      js: {
        src: ['node_modules/jquery/dist/jquery.js', 'node_modules/popper.js/dist/umd/popper.js', 'node_modules/bootstrap/dist/js/bootstrap.js'],
        dest: 'dist/assets/scripts/vendor.js'
      },
      css: {
        src: ['node_modules/bootstrap/dist/css/bootstrap.css'],
        dest: 'dist/assets/styles/vendor.css'
      },

    },
    copy: {
      main: {
        expand: true,
        cwd: 'public',
        src: '*',
        dest: 'dist/',
      },
    },
    useminPrepare: {
      html: 'dist/*.html',
      options: {
        dest: 'dist'
      }
    },
    usemin: {
      html: 'dist/*.html',
      options: {
        dest: 'dist'
      }
    },
    watch: {
      scss: {
        files: 'src/assets/styles/*.scss',
        tasks: ['sass']
      }
    },
    browserSync: {
      bsFiles: {
        src: 'public'
      },
      options: {
        watchTask: true,
        server: {
          baseDir: "dist"
        }
      }
    }
  })

  // 自动载入插件
  loadGruntTasks(grunt)
  // 组合任务
  grunt.registerTask('cleanAll', ['clean'])
  grunt.registerTask('dev', ['clean', 'sass', 'babel', 'concat', 'copy','imagemin', 'swigtemplates', 'useminPrepare', 'usemin', 'browserSync', 'watch'])
  grunt.registerTask('build', ['clean', 'sass', 'babel', 'concat', 'copy','imagemin', 'swigtemplates', 'useminPrepare', 'usemin'])
}
